package com.why.amallrest.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.why.common.utils.PageUtils;
import com.why.common.utils.Result;
import com.why.entity.UserAddrEntity;
import com.why.service.UserAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;




/**
 * 用户地址 
 *
 * @author 夏沫止水
 * @email HeJieLin@gulimall.com
 * @date 2021-11-06 13:25:10
 */
@RestController
@RequestMapping("/mall/userAddr")
public class UserAddrController {
    @Autowired
    private UserAddrService userAddrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions(":useraddr:list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtils page = userAddrService.queryPage(params);

        return Result.ok().put("page", page);
    }


    /**
     * 根据userId查询用户地址
     */
    @RequestMapping("/info/{userId}")
    public Result info(@PathVariable("userId") String userId){

        QueryWrapper<UserAddrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<UserAddrEntity> userAddrEntities = userAddrService.list(wrapper);
        return Result.ok().put("data", userAddrEntities);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody UserAddrEntity userAddr){
		userAddrService.save(userAddr);

        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions(":useraddr:update")
    public Result update(@RequestBody UserAddrEntity userAddr){
		userAddrService.updateById(userAddr);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions(":useraddr:delete")
    public Result delete(@RequestBody String[] addrIds){
		userAddrService.removeByIds(Arrays.asList(addrIds));

        return Result.ok();
    }

}
